<%--
  Created by IntelliJ IDEA.
  User: lt
  Date: 2018/6/21
  Time: 21:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>数据统计 -- 采购汇总数据</title>
    <link rel="stylesheet" href="${contextPath}/static/statistics/css/bootstrap.min.css">
    <link rel="stylesheet" href="${contextPath}/static/statistics/css/bootstrap-datetimepicker.min.css">
    <link rel="stylesheet" href="${contextPath}/static/layer/css/layui.css">
    <script src="${contextPath}/static/statistics/js/jquery.js"></script>
    <script src="${contextPath}/static/statistics/js/bootstrap.min.js"></script>
    <script src="${contextPath}/static/statistics/js/bootstrap-datetimepicker.min.js"></script>
    <script src="${contextPath}/static/statistics/js/bootstrap-datetimepicker.fr.js"></script>
    <script src="${contextPath}/static/statistics/js/bootstrap-datetimepicker.zh-CN.js"></script>
    <script src="${contextPath}/static/statistics/js/vue.js"></script>
    <script src="${contextPath}/static/statistics/js/echarts.min.js"></script>
    <script type="text/javascript" src="${contextPath}/static/statistics/js/jquery.mockjax.js"></script>
    <script type="text/javascript" src="${contextPath}/static/statistics/js/jquery.autocomplete.js"></script>

    <link rel="stylesheet" href="${contextPath}/static/XiaLaDuoXuan/css/public.css" media="all" />

    <link href="${contextPath}/static/ztree/css/zTreeStyle.css" rel="stylesheet"/>
    <script type="text/javascript" src="${contextPath}/static/ztree/js/jquery.ztree.core-3.5.js"></script>
    <script type="text/javascript" src="${contextPath}/static/ztree/js/jquery.ztree.excheck-3.5.js"></script>

    <style>
        .autocomplete-suggestions {
            border: 1px solid #999;
            background: #FFF;
            overflow: auto;
        }

        .autocomplete-suggestion {
            padding: 2px 5px;
            white-space: nowrap;
            overflow: hidden;
        }

        .autocomplete-selected {
            background: #F0F0F0;
        }

        .autocomplete-suggestions strong {
            font-weight: normal;
            color: #3399FF;
        }

        .layui-input{
            height: 30px;
        }
        .multiSelect{
            margin-top: -33px;
        }
    </style>
</head>
<body style="background: rgba(119, 118, 118, 0.15);">
<div class="container-fluid" id="app">
    <form class="form-inline layui-form" style="margin-top: 50px; margin-bottom: 5px;">
        <%--<select class="input-sm" id="shopId" style="width: 140px; height: 32px;">--%>
            <%--&lt;%&ndash;<option >选择店铺</option>&ndash;%&gt;--%>
            <%--<option :value="obj.shopId" v-for="(obj, index) in arrayShop">{{ obj.shopName }}</option>--%>
        <%--</select>--%>
        <%--<div class="form-group">--%>
            <%--<input id="shopId" onfocus="this.data=''" class="form-control input-sm hide" type="text" placeholder="店铺" style="width: 280px;">--%>
            <%--<input id="shopNameId" onfocus="this.value=''" class="form-control input-sm" type="text" placeholder="店铺" style="width: 280px;">--%>
        <%--</div>--%>

        <div class="form-group">
            <input id="supplierId" onfocus="this.data=''" class="form-control input-sm hide" type="text" placeholder="供应商id" style="width: 280px;">
            <input id="supplierNameId" onfocus="this.value=''" class="form-control input-sm" type="text" placeholder="供应商" style="width: 280px;">
        </div>

        <%--<div class=" form-group">--%>
            <%--<select lay-filter="bbb" multiple name="bbb" lay-verify="required" class="" id="shoptype" placeholder="店铺">--%>
                <%--<option value="" disabled>商品类型</option>--%>

                <%--<option :value="obj.shopId" v-for="(obj, index) in arrayShop">{{ obj.shopName }}</option>--%>
            <%--</select>--%>
        <%--</div>--%>

        <div class="form-group">
            <input type="text" class="form-control" id="turnCreateDate" placeholder="查询开始时间">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" id="bindCreateDate" placeholder="查询结束时间">
        </div>
        <button type="button" class="btn btn-success" onclick="queryOrder();">查询</button>
        <button type="button" title="根据查询条件导出所有记录" onclick="exportExcel();"
                class="btn btn-info">导出Excel
        </button>
        <button type="button" class="btn btn-success" onclick="doPrint();" style="margin-top: 0px;">打印</button>
        <button type="button" class="btn btn-success" onclick="javascript:window.history.go(-2);window.close();">返回</button>
    </form>
    <div class="row" style="background-color: rgb(53, 147, 202);margin-right: 0px;margin-left: 0px; height: 37px;">
        <div class="col-sm-10" style="margin-top: 6px;">
            <strong style="font-size: 18px;color: #f9f9f9;">汇总数据</strong>
        </div>
        <div class="col-sm-2" style="text-align: right">

        </div>
    </div>
    <div class="table-responsive" id="tableContainer" style="height: 600px">
        <!--startprint-->
        <table class="table table-striped table-condensed table-bordered" style="width: 1300px;">
            <thead style="background-color:#eee;">
            <tr>
                <th>序号</th>
                <%--<th>店铺ID</th>--%>
                <%--<th>店铺名称</th>--%>
                <th>供应商ID</th>
                <th>供应商名称</th>
                <th>进货金额</th>
                <th>退货金额</th>
                <th>实际金额</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(order,index) in orderList">
                <td>{{ index + 1 }}</td>
                <%--<td>{{order.REPERTORY_ID}}</td>--%>
                <%--<td>{{order.SHOP_NAME }}</td>--%>
                <td>{{order.SUPPLIER_ID}}</td>
                <td>{{order.SUPPLIER_NAME}}</td>
                <td><a :name="order.REPERTORY_ID" :id="order.SUPPLIER_ID" onclick="toKtInsCargo(this.name, this.id);" style="color: #3264C3;text-decoration: underline">{{order.AMTIN }}</a></td>
                <td><a :name="order.REPERTORY_ID" :id="order.SUPPLIER_ID" onclick="toKtOutCargo(this.name, this.id);" style="color: #3264C3;text-decoration: underline">{{order.AMTBACK }}</a></td>
                <td>{{order.FACTAMT }}</td>
            </tr>
            </tbody>
        </table>
        <!--endprint-->
    </div>
    <div style="text-align: right;margin-top: 10px;" id="pageDiv"></div>
</div>


<%--<form action="${contextPath}/ktCollect/ktOutCargo" method="post" target="_blank" id="goToDetailPage"--%>
      <%--style="display:none">--%>
    <%--<input type="text" id="shopIds" name="shopIds" value="">--%>
<%--</form>--%>

<form action="${contextPath}/ktcount/exportExcel" method="post" id="exportExcel" style="display:none">
    <input type="hidden" name="turnCreateDate" id="exportExcelParamTurnCreateDate">
    <input type="hidden" name="bindCreateDate" id="exportExcelParamBindCreateDate">
    <input type="hidden" name="supplierId" id="exportExcelParamSupplierId">
    <input type="hidden" name="tableType" value="ktCollect">
</form>
</body>

<!-- 打印 -->
<script type="text/javascript">
    function doPrint() {
        bdhtml=window.document.body.innerHTML;
        sprnstr="<!--startprint-->";
        eprnstr="<!--endprint-->";
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
        window.document.body.innerHTML=prnhtml;
        window.print();
        window.location.reload(); //刷新当前页面
    }
</script>

<script src=${contextPath}/static/layer/layui.all.js></script>
<script type="text/javascript">
    var turnTime = format(new Date(),"yyyy_MM_dd");
    var time = format(new Date(),"yyyy-MM-dd");

//    var shopId = '';

    var laypage = layui.laypage
        , layer = layui.layer;


    var queryModel = {
        turnCreateDate: turnTime,
        bindCreateDate: time,
//        shopId: '',
        supplierId: '',
    }

    var app = new Vue({
        el: '#app',
        data: {
            orderList: [],
            arrayShop: [],
        }
    })

    var getOrder = function (queryModel) {
        console.log(queryModel);
        layer.load(1)
        $.ajax({
            url: '${contextPath}/ktcount/getktCollect', //localhost:8080
            type: 'post',
            data: queryModel,
            success: function (data) {
                layer.closeAll('loading');
                if (data.success == false) {
                    layer.msg("获取出错：" + data.message)
                    return
                }
                //console.log(data)
                var dataArray = data.data;
                if (dataArray.length == 0) {
                    layer.msg("查询没有数据")
                }
//                dataArray = qui(dataArray);
                app.orderList = dataArray
            },
            error: function (error) {
                layer.closeAll('loading');
                ////console.log(error)
            }
        })
    }

    //获取店铺
    var getShop = function () {
        var shopQuery = {};
        var filters = {
            "rules": [
                {
                    "field": "type",
                    "data": 'kt'
                },
                {
                    "field" : "state",
                    "data": '1'
                }
            ]
        };

        shopQuery.shopType = "'加盟', '直营'";
        shopQuery.filters = JSON.stringify(filters);
        shopQuery.state = '5';
        $.ajax({
            url: '${contextPath}/ktCommon/getShopList2User',
            type: 'post',
            data: shopQuery,
            success: function (data) {
                //console.log(data);
                var shopArray = []
                for(var i = 0; i < data.length; i++){
                    var shop = {}
                    shop.value = data[i].shopName;
                    shop.data = data[i].shopId;
                    shopArray.push(shop);
                }
                var countries = shopArray

                //console.log(countries);
                $('#shopNameId').autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        $("#shopId").val(suggestion.data);
                        // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);

                    },
                    minChars: 0,
                });
            },
//            success: function (data) {
//                if(data.length == 0){
//                    return;
//                }
//                var dataArray = data;
//                app.arrayShop = dataArray;
//
////                queryModel.shopId = data[0].shopId;
////                queryModel.shopId = '2889';
//
//            },
            error: function (error){
                get();
                layer.msg("获取出错："+data.message)
            }
        })
    }

    var getSupplier = function () {
        var parameter = {page: '1', rows: '1000'}
        $.ajax({
            url: '${contextPath}/ktPurchase/getSupplierListByPage',
            type: 'post',
            data: parameter,
            success: function (data) {
//                console.log(data)
////                layer.closeAll('loading');
//                if (data.success == false) {
//                    layer.msg("获取出错：" + data.message)
//                    layer.closeAll('loading')
//                    return
//                }
                var dataArray = data.recordList;
//                app.supplierObj = dataArray;


                var supplierArray = [];
                for(var i = 0; i < dataArray.length; i++){
                    var supplier = {}
                    supplier.value = dataArray[i].supplierName;
                    supplier.data = dataArray[i].id;
                    supplierArray.push(supplier);
                }
                var countries = supplierArray

                console.log(countries);
                $('#supplierNameId').autocomplete({
                    lookup: countries,
                    onSelect: function (suggestion) {
                        $("#supplierId").val(suggestion.data);
                        // alert('You selected: ' + suggestion.value + ', ' + suggestion.data);

                    },
                    minChars: 0,
                });

            },
            error: function (error) {
                layer.closeAll('loading')
//                layer.closeAll('loading')
            }
        })
    }

    var queryOrder = function () {
        queryModel.turnCreateDate = $("#turnCreateDate").val()
        queryModel.bindCreateDate = $("#bindCreateDate").val()
        queryModel.supplierId = $("#supplierId").val()
        queryModel.shopId = $("#shopId").val()
        getOrder(queryModel)
    }

    //页面加载完后调用
    $(function () {
        getShop();
        queryOrder();
        getSupplier();    //获取供应商列表
    })


    <%--$.ajax({--%>
    <%--url: '${contextPath}/checking/getTbcocompNamesAndBankNamesAndMerNames', //--%>
    <%--type: 'post',--%>
    <%--data: queryModel,--%>
    <%--success: function (data) {--%>
    <%--if (data.success == false) {--%>
    <%--layer.msg("获取查询条件出错，请刷新页面")--%>
    <%--return--%>
    <%--}--%>
    <%--//////console.log(data)--%>
    <%--var dataObj = data.data;--%>
    <%--app.turnCreateDate = dataObj.turnCreateDate--%>
    <%--app.bindCreateDate = dataObj.bindCreateDate--%>
    <%--},--%>
    <%--error: function (error) {--%>
    <%--////console.log(error)--%>
    <%--layer.msg("获取查询条件出错，请刷新页面")--%>
    <%--}--%>
    <%--})--%>


    $('#turnCreateDate').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
//        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        minuteStep:1
//        startDate:new Date(),
    });


    $('#bindCreateDate').datetimepicker({
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,
//        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1,
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        minuteStep:1
//        startDate:new Date(),
    });



    var exportExcel = function () {
        $("#exportExcelParamTurnCreateDate").val(queryModel.turnCreateDate)
        $("#exportExcelParamBindCreateDate").val(queryModel.bindCreateDate)
        $("#exportExcelParamSupplierId").val(queryModel.supplierId)
        $("#exportExcel").submit();
    }


    //***************** 采购明细 ************
    function toKtInsCargo(shopId, supplierId){
        var turnCreateDate = $("#turnCreateDate").val();
        var bindCreateDate = $("#bindCreateDate").val();
        window.location.href = "${contextPath}/ktcount/ktInsCargo?supplierId="+supplierId+"&turnCreateDate="+turnCreateDate+"&bindCreateDate="+bindCreateDate;
    }
    //***************** 退货明细 ************
    function toKtOutCargo(shopId, supplierId){
        var turnCreateDate = $("#turnCreateDate").val();
        var bindCreateDate = $("#bindCreateDate").val()
        window.location.href = "${contextPath}/ktcount/ktOutCargo?supplierId="+supplierId+"&turnCreateDate="+turnCreateDate+"&bindCreateDate="+bindCreateDate;
    }

    //*******************************

    function qui(orderList) {
        var shortName = '合计';
        var orderNo = 0;
        var ordAmtSum = 0;
        var successOrderNo = 0;
        var successOrdAmtSum = 0;
//        //console.log("orderList="+orderList.length);
        for (var i=0; i<orderList.length; i++){
            orderNo = orderList[i].orderNo + orderNo;
            ordAmtSum = orderList[i].ordAmtSum + ordAmtSum;
            successOrderNo = orderList[i].successOrderNo + successOrderNo;
            successOrdAmtSum = orderList[i].successOrdAmtSum + successOrdAmtSum;
        }
        var map = {"shortName" : shortName, "orderNo" : orderNo, "ordAmtSum" : ordAmtSum,
            "successOrderNo" : successOrderNo, "successOrdAmtSum" : successOrdAmtSum}
//        //console.log("map="+map.shortName+" "+map.ordAmtSum);
        orderList.push(map);
        return orderList;
    }

    //******************* 表头固定 *********************
    // Code goes here
    'use strict'
    window.onload = function(){
        var tableCont = document.querySelector('#tableContainer')
        /**
         * scroll handle
         * @param {event} e -- scroll event
         */
        function scrollHandle (e){
            //console.log(this)
            var scrollTop = this.scrollTop;
            this.querySelector('thead').style.transform = 'translateY(' + scrollTop + 'px)';
        }

        tableCont.addEventListener('scroll',scrollHandle)
    }

    //****************************************

    $('#turnCreateDate').val(turnTime);
    $('#bindCreateDate').val(time);
    function format(date,str){
        var mat={};
        mat.M=date.getMonth()+1;//月份记得加1
        mat.H=date.getHours();
        mat.s=date.getSeconds();
        mat.m=date.getMinutes();
        mat.Y=date.getFullYear();
        mat.D=date.getDate();
        mat.d=date.getDay();//星期几
        mat.d=check(mat.d);
        mat.H=check(mat.H);
        mat.M=check(mat.M);
        mat.D=check(mat.D);
        mat.s=check(mat.s);
        mat.m=check(mat.m);
        if(str.indexOf(":")>-1){
            mat.Y=mat.Y.toString().substr(2,2);
            return mat.Y+"/"+mat.M+"/"+mat.D;
        }
        if(str.indexOf("/")>-1){
            return mat.Y+"/"+mat.M+"/"+mat.D+" "+mat.H+"/"+mat.m+"/"+mat.s;
        }
        if(str.indexOf("-")>-1){
            return mat.Y+"-"+mat.M+"-"+mat.D;
        }
        if(str.indexOf("_")>-1){
            return mat.Y+"-"+mat.M+"-01";
        }
    }
    //检查是不是两位数字，不足补全
    function check(str){
        str=str.toString();
        if(str.length<2){
            str='0'+ str;
        }
        return str;
    }
</script>
</html>